package com.youlei.newgraduateproject.common.easyexcel.converter;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * @author youlei
 * @version 1.0
 * @Date 2021-06-02-15:58
 * @Description
 */

/**
 * 自定义LocalDateStringConverter
 * 用于解决使用easyexcel导出表格时候，默认不支持LocalDateTime日期格式
 *
 * 在需要的属性上添加注解 @ExcelProperty(value = "入职时间", converter = LocalDateStringConverter.class)
 */

public class ListStringConverter implements Converter<List<Long>> {

    /**
     * Back to object types in Java
     *
     * @return Support for Java class
     */
    @Override
    public Class supportJavaTypeKey() {
        return List.class;
    }

    /**
     * Back to object enum in excel
     *
     * @return Support for {@link CellDataTypeEnum}
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * Convert excel objects to Java objects
     *
     * @param cellData            Excel cell data.NotNull.
     * @param contentProperty     Content property.Nullable.
     * @param globalConfiguration Global configuration.NotNull.
     * @return Data to put into a Java object
     * @throws Exception Exception.
     */
    @Override
    public List<Long> convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String[] values = cellData.getStringValue().split(";");
        List<Long> list = new ArrayList<>();
        for (String value:values) {
            long l = Long.parseLong(value);
            list.add(l);
        }
        return list;
    }

    /**
     * Convert Java objects to excel objects
     *
     * @param orderitemsIds               Java Data.NotNull.
     * @param contentProperty     Content property.Nullable.
     * @param globalConfiguration Global configuration.NotNull.
     * @return Data to put into a Excel
     * @throws Exception Exception.
     */
    @Override
    public CellData convertToExcelData(List<Long> orderitemsIds, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value_String="";
        for (Long orderitemsId:orderitemsIds) {
            String s = String.valueOf(orderitemsId);
            value_String=value_String.concat(s+";");
        }

        return new CellData(value_String);
    }
}

